<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body {
        background-color: #f5f5f5;
    }

    #box {
        width: 1120px;
        margin: 100px auto;
        background-color: #fff;
        padding: 50px;
    }

    ul,
    ol {
        list-style: none;
    }

    ul {
        display: flex;
        margin-bottom: 50px;
    }

    ul li {
        color: red;
        border: 1px solid red;
        margin-right: 15px;
        font-size: 14px;
        padding: 5px 5px;
        cursor: pointer;
    }

    .bg-red {
        background-color: red;
        color: #fff;
    }

    .main {
        display: none;
    }

    ol {
        overflow: hidden;
        display: flex !important;
        flex-wrap: wrap;
    }

    ol li {
        width: 240px;
        height: 120px;
        position: relative;
        overflow: hidden;
        margin-right: 20px;
        margin-bottom: 40px;
    }

    ol li:nth-child(4n) {
        margin-right: 0px;
    }

    img {
        width: 100%;
        /* height: 100%; */
        transform: translate(0,-50%);
        transition: all .5s;
    }

    ol li .mask {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        /* height: 100%; */
        text-align: center;
        line-height: 120px;
        color: #fff;
        font-size: 26px;
        background-color: rgba(0, 0, 0, 0.6);
        opacity: 0;
        transition: all .5s;
    }

    ol li:hover .mask {
        opacity: 1;
        z-index: 2;
    }

    ol li:hover img {
        transform: scale(1.2);
    }

    .show {
        display: block;
    }

</style>

<body>
    <div id="box">
        <ul>
            <li class="bg-red">全部</li>
            <li>热血</li>
            <li>冒险</li>
            <li>竞技</li>
        </ul>
    </div>
    </div>
    <script src="./data.js"></script>
    <script>

        console.log(data);

        const ulLis = Array.from(document.getElementsByTagName('ul')[0].children)
        const box = document.getElementById('box')


        for (let i = 0; i < 4; i++) {
            let div = document.createElement('div')
            div.classList.add('main')
            if (i === 0) div.classList.add('show')
            div.innerHTML = `
                <ol>
                </ol>
            `
            box.append(div)
        }

        const ols = document.getElementsByClassName('main')
        render('全部', ols[0].children[0])
        let index = 0
        ulLis.forEach((v, i) => {
            v.onclick = () => {
                ulLis[index].classList.remove('bg-red')
                v.classList.add('bg-red')

                ols[index].classList.remove('show')
                ols[i].classList.add('show')
                render(v.innerHTML, ols[i].children[0])
                index = i
            }
        })

        function render(str, ele) {
            ele.innerHTML = ''
            data.forEach((v, i) => {
                if (str === '全部') {
                    let li = document.createElement('li')
                    li.innerHTML = `
                        <img src=${v.src} alt=${v.alt}>
                        <div class="mask">${v.title}</div>
                    `
                    ele.append(li)
                } else if (str === v.type) {
                    let li = document.createElement('li')
                    li.innerHTML = `
                        <img src=${v.src} alt=${v.alt}>
                        <div class="mask">${v.title}</div>
                    `
                    ele.append(li)
                }
            })
        }



    </script>
</body>

</html>